//
//  OneViewController.m
//  ceshi
//
//  Created by 乐辽科技 on 2018/6/14.
//  Copyright © 2018年 乐辽科技. All rights reserved.
//

#import "OneViewController.h"
#import "UIImage+GIF.h"

@interface OneViewController ()
@property (weak, nonatomic) IBOutlet UITextField *text;
@property (nonatomic, strong)UIImageView *loadingImageView;

@end

@implementation OneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.text.text = self.name;
    NSLog(@"%@",_name);
//    // 设定位置和大小
//    CGRect frame = CGRectMake(0,0,333,333);
//    frame.size = [UIImage imageNamed:@"1694361.gif"].size;
//    // 读取gif图片数据
//    NSData *gif = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"1694361" ofType:@"gif"]];
//    // view生成
//    UIWebView *webView = [[UIWebView alloc] initWithFrame:frame];
//    webView.userInteractionEnabled = NO;//用户不可交互
//    [webView loadData:gif MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
//    [self.view addSubview:webView];
//    [webView release];
    self.loadingImageView= [[UIImageView alloc]init];
    UIButton* btn=[[UIButton alloc]initWithFrame:CGRectMake(0, 100, 50, 20)];
    btn.backgroundColor=[UIColor redColor];
    [self.view addSubview:btn];
//    [btn addTarget:self action:@selector(initLoadingImageView) forControlEvents:UIControlEventTouchUpInside];
    [btn addTarget:self action:@selector(initLoadingImageViewS) forControlEvents:UIControlEventTouchUpInside];

}
-(void)initLoadingImageViewS
{
    UIImageView *gifImageView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    NSArray *gifArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"1"],
                         [UIImage imageNamed:@"2"],
                         [UIImage imageNamed:@"3"],
                         [UIImage imageNamed:@"4"],
                         [UIImage imageNamed:@"5"],
                         [UIImage imageNamed:@"6"],
                         [UIImage imageNamed:@"7"],
                         [UIImage imageNamed:@"8"],
                         [UIImage imageNamed:@"9"],
                         [UIImage imageNamed:@"10"],
                         [UIImage imageNamed:@"11"],
                         [UIImage imageNamed:@"12"],
                         [UIImage imageNamed:@"13"],
                         [UIImage imageNamed:@"14"],
                         [UIImage imageNamed:@"15"],
                         [UIImage imageNamed:@"16"],
                         [UIImage imageNamed:@"17"],
                         [UIImage imageNamed:@"18"],
                         [UIImage imageNamed:@"19"],
                         [UIImage imageNamed:@"20"],
                         [UIImage imageNamed:@"21"],
                         [UIImage imageNamed:@"22"],nil];
    gifImageView.animationImages = gifArray; //动画图片数组
    gifImageView.animationDuration = 5; //执行一次完整动画所需的时长
    gifImageView.animationRepeatCount = 1;  //动画重复次数
    [gifImageView startAnimating];
    [self.view addSubview:gifImageView];
//    [gifImageView release];
}

//生产图片
-(NSArray *)productionIMageage
{
//    Production pictures
}
//2.初始化按钮，（loadingImageView是定义的全局的UIImageView，自己顶一下）


- (UIImage *)removeWhiteColorWithImage:(UIImage *)image{
    return [self removeColorWithMaxR:255 minR:250 maxG:255 minG:240 maxB:255 minB:240 image:image];
}
- (UIImage *)removeBlackColorWithImage:(UIImage *)image{
    return [self removeColorWithMaxR:15 minR:0 maxG:15 minG:0 maxB:15 minB:0 image:image];
}

//3.点击按钮就可以实现了，非常简单

- (UIImage *)removeColorWithMaxR:(float)maxR minR:(float)minR maxG:(float)maxG minG:(float)minG maxB:(float)maxB minB:(float)minB image:(UIImage *)image{
    // 分配内存
    const int imageWidth = image.size.width;
    const int imageHeight = image.size.height;
    size_t bytesPerRow = imageWidth * 4;
    uint32_t* rgbImageBuf = (uint32_t*)malloc(bytesPerRow * imageHeight);
    // 创建context
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();// 色彩范围的容器
    CGContextRef context = CGBitmapContextCreate(rgbImageBuf, imageWidth, imageHeight, 8, bytesPerRow, colorSpace,kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
    CGContextDrawImage(context, CGRectMake(0, 0, imageWidth, imageHeight), image.CGImage);
    // 遍历像素
    int pixelNum = imageWidth * imageHeight;
    uint32_t* pCurPtr = rgbImageBuf;
    for (int i = 0; i < pixelNum; i++, pCurPtr++)
    {
        uint8_t* ptr = (uint8_t*)pCurPtr;
        if (ptr[3] >= minR && ptr[3] <= maxR &&
            ptr[2] >= minG && ptr[2] <= maxG &&
            ptr[1] >= minB && ptr[1] <= maxB) {
            ptr[0] = 0;
        }else{
            printf("\n---->ptr0:%d ptr1:%d ptr2:%d ptr3:%d<----\n",ptr[0],ptr[1],ptr[2],ptr[3]);
        }
    }
    // 将内存转成image
    CGDataProviderRef dataProvider =CGDataProviderCreateWithData(NULL, rgbImageBuf, bytesPerRow * imageHeight, nil);
    CGImageRef imageRef = CGImageCreate(imageWidth, imageHeight,8, 32, bytesPerRow, colorSpace,kCGImageAlphaLast |kCGBitmapByteOrder32Little, dataProvider,NULL,true,kCGRenderingIntentDefault);
    CGDataProviderRelease(dataProvider);
    UIImage* resultUIImage = [UIImage imageWithCGImage:imageRef];
    // 释放
    CGImageRelease(imageRef);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    return resultUIImage;
}

- (void)initLoadingImageView
{
    
    NSString  *name = @"chengbao.gif";
    NSString  *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:name ofType:nil];
    NSData  *imageData = [NSData dataWithContentsOfFile:filePath];
    
    if (!self.loadingImageView) {
        self.loadingImageView = [[UIImageView alloc]init];
    }
    self.loadingImageView.backgroundColor = [UIColor clearColor];
    
    self.loadingImageView.image = [self removeWhiteColorWithImage:[UIImage sd_animatedGIFWithData:imageData]];
    
    //这里可以修改要实现播放的gif的frame
    self.loadingImageView.frame = CGRectMake(0, 100, 200, 100);
    
    [self.view addSubview:self.loadingImageView];
    
    [self.view bringSubviewToFront:self.loadingImageView];
    
}



- (IBAction)返回:(UIButton *)sender {
    
    //代理
    if(self.delegate && [self.delegate respondsToSelector:@selector(textData:)])
    {
        [self.delegate textData:self.text.text];
        
    }
    //block
//    _data(self.text.text);
    
    //单例
    self.dataText = self.text.text;
    
    //通知传值
//    NSNotification *nt = [[NSNotification alloc] initWithName:@"dataTextNotification" object:nil userInfo:@{@"dataTextNotification":self.text.text}];
//    [[NSNotificationCenter defaultCenter] postNotification:nt];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"dataTextNotification" object:nil userInfo:@{@"dataTextNotification":self.text.text}];
   [self.navigationController popToRootViewControllerAnimated:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
